/**
 * 调试工具
 * 用于在安卓环境下排查问题
 */

// 调试开关
const DEBUG = true

// 日志输出
export const log = (...args) => {
    if (DEBUG) {
        console.log('[DEBUG]', ...args)
    }
}

// 错误日志
export const logError = (...args) => {
    if (DEBUG) {
        console.error('[ERROR]', ...args)
    }
}

// 警告日志
export const logWarn = (...args) => {
    if (DEBUG) {
        console.warn('[WARN]', ...args)
    }
}

// 网络请求日志
export const logNetwork = (type, url, data) => {
    if (DEBUG) {
        console.log(`[NETWORK ${type}]`, url, data)
    }
}

// 页面生命周期日志
export const logPageLifecycle = (pageName, lifecycle) => {
    if (DEBUG) {
        console.log(`[PAGE ${lifecycle}]`, pageName)
    }
}

// 检查环境
export const checkEnvironment = () => {
    const info = {
        platform: uni.getSystemInfoSync().platform,
        version: uni.getSystemInfoSync().version,
        SDKVersion: uni.getSystemInfoSync().SDKVersion,
        language: uni.getSystemInfoSync().language,
        pixelRatio: uni.getSystemInfoSync().pixelRatio,
        screenWidth: uni.getSystemInfoSync().screenWidth,
        screenHeight: uni.getSystemInfoSync().screenHeight,
        windowWidth: uni.getSystemInfoSync().windowWidth,
        windowHeight: uni.getSystemInfoSync().windowHeight,
        statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
        safeArea: uni.getSystemInfoSync().safeArea
    }
    
    if (DEBUG) {
        console.log('[ENVIRONMENT]', info)
    }
    
    return info
}

// 检查网络状态
export const checkNetworkStatus = () => {
    uni.getNetworkType({
        success: (res) => {
            if (DEBUG) {
                console.log('[NETWORK]', res)
            }
        },
        fail: (err) => {
            if (DEBUG) {
                console.error('[NETWORK ERROR]', err)
            }
        }
    })
}

// 检查存储
export const checkStorage = () => {
    try {
        const token = uni.getStorageSync('token')
        const refreshToken = uni.getStorageSync('refreshToken')
        const userId = uni.getStorageSync('userId')
        
        if (DEBUG) {
            console.log('[STORAGE]', {
                hasToken: !!token,
                hasRefreshToken: !!refreshToken,
                hasUserId: !!userId
            })
        }
        
        return {
            hasToken: !!token,
            hasRefreshToken: !!refreshToken,
            hasUserId: !!userId
        }
    } catch (error) {
        if (DEBUG) {
            console.error('[STORAGE ERROR]', error)
        }
        return {
            hasToken: false,
            hasRefreshToken: false,
            hasUserId: false
        }
    }
}

export default {
    log,
    logError,
    logWarn,
    logNetwork,
    logPageLifecycle,
    checkEnvironment,
    checkNetworkStatus,
    checkStorage
}
